package com.zxx.flowerstore.controller;

import com.alibaba.fastjson.JSONObject;
import com.zxx.flowerstore.entity.FlowerUser;
import com.zxx.flowerstore.entity.enums.ResultEnum;
import com.zxx.flowerstore.service.UserService;
import com.zxx.flowerstore.utils.common.MyException;
import com.zxx.flowerstore.utils.common.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @ClassName UserController
 * @Author Admin
 * @Date 2021/3/16 20:38
 * @Description UserController
 * @Version 1.0
 */
@RestController
@RequestMapping("/user")
@CrossOrigin
public class UserController {

    @Autowired
    private UserService userService;

    private static final Logger logger = LoggerFactory.getLogger(UserController.class);

    /**
     *  注册
     * @param flowerUser
     * @return
     */
    @PostMapping("/register")
    @ResponseBody
    public Result register(@RequestBody FlowerUser flowerUser) {
        try {
            return userService.register(flowerUser);
        } catch (MyException e) {
            logger.info("用户注册:"+e.getMessage());
            return Result.error(ResultEnum.ERROR.getCode(), e.getMessage());
        } catch (Exception e) {
            logger.error("用户注册:"+e.getMessage());
            return Result.error(ResultEnum.ERROR.getCode(), "注册失败");
        }
    }

    @PostMapping("/sendEmailCode")
    @ResponseBody
    public Result sendEmailCode(@RequestBody FlowerUser flowerUser) {
        try {
            return userService.sendEmailCode(flowerUser);
        } catch (MyException e) {
            logger.info("用户注册:"+e.getMessage());
            return Result.error(ResultEnum.ERROR.getCode(), e.getMessage());
        } catch (Exception e) {
            logger.error("发送邮件验证码::"+e.getMessage());
            return Result.error(ResultEnum.ERROR.getCode(), "发送邮件验证码失败");
        }
    }


    /**
     *  登录
     * @param flowerUser
     * @return
     */
    @PostMapping("/login")
    @ResponseBody
    public Result login(@RequestBody FlowerUser flowerUser) {
        try {
            return userService.login(flowerUser);
        } catch (MyException e) {
            logger.info("用户登录:"+e.getMessage());
            return Result.error(ResultEnum.ERROR.getCode(), e.getMessage());
        } catch (Exception e) {
            logger.error("用户登录::"+e.getMessage());
            return Result.error(ResultEnum.ERROR.getCode(), "登录失败");
        }
    }


    /**
     * @// TODO: 2021/3/24
     *  前端验证码 待定
     */
    @RequestMapping("/getVerifyImg")
    @ResponseBody
    public void getVerifyImg() {

    }

    /**
     *  忘记密码
     * @param flowerUser
     * @return
     */
    @PostMapping("/forgetPassword")
    @ResponseBody
    public Result forgetPassword(@RequestBody FlowerUser flowerUser) {
        try {
            return userService.forgetPassword(flowerUser);
        } catch (MyException me) {
            logger.info("忘记密码:"+me.getMessage());
            return Result.error(ResultEnum.ERROR.getCode(), me.getMessage());
        } catch (Exception e) {
            logger.info("忘记密码:"+e.getMessage());
            return Result.error(ResultEnum.ERROR.getCode(), "修改密码失败");
        }
    }


    /**
     *  修改密码
     * @param flowerUser
     * @return
     */
    @PostMapping("/updatePassword")
    @ResponseBody
    public Result updatePassword(@RequestBody FlowerUser flowerUser) {
        try {
            return userService.updatePassword(flowerUser);
        } catch (MyException me) {
            logger.info("修改密码:"+me.getMessage());
            return Result.error(ResultEnum.ERROR.getCode(), me.getMessage());
        } catch (Exception e) {
            logger.error("修改密码:"+e.getMessage());
            return Result.error(ResultEnum.ERROR.getCode(), "修改密码失败");
        }
    }


    @PostMapping("/selectUserList")
    @ResponseBody
    public Result selectUserList() {

        return null;
    }


    @PostMapping("/userInfo")
    @ResponseBody
    public JSONObject userInfo() {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("status",false);
        return jsonObject;
    }

}
